<template>
	<view>
		<cu-custom bgColor="bg-blue">
			<block slot="content">首页</block>
		</cu-custom>
		<view class="cu-bar bg-white search fixed" :style="[{top:CustomBar + 'px'}]">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input type="text" placeholder="搜索" confirm-type="search" @input="searchIcon"></input>
			</view>
		</view>
		<view style="width: 100vw;">
			<view style="height: 7vh;padding: 0 25rpx;margin: 30rpx 0;" v-for="(item,index) in cuIcon" :key="index"
				v-if="item.isShow">
				<view
					style="height: 100%;background-color: white;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);border-radius: 60rpx;display: flex;overflow: hidden;">
					<view style="width: 20%;height: 100%;display: flex;justify-content: center;align-items: center;"
						@tap="goNavs(item.Asrc)">
						<img :src="item.imgSrc" alt="" style="width: 100rpx;height: 100rpx;" />
					</view>
					<view
						style="width: 68%;display: flex;align-items: center;font-size: 33rpx;font-weight: 700;color: #5a5a5b;"
						@tap="goNavs(item.Asrc)">
						{{item.name}}
					</view>
					<view style="display: flex;flex-direction: column;align-items: center;justify-content: center;"
						@tap="goDing(item)">
						<view class="">
							<img src="@/static/dingwei.png" alt="" style="width: 45rpx;height: 45rpx;" />
						</view>
						<text style="color: #23AAF2;font-size: 23rpx;">来院</text>
					</view>
				</view>
			</view>
		</view>
		<view
			style="width: 100vw;display: flex;justify-content: center;align-items: center;position: fixed;bottom: 10%;left: 50%;transform: translate(-50%,-50%);z-index: -1;">
			<img src="@/static/huerai.png" alt="" style="width: 40rpx;height: 40rpx;border-radius: 8rpx;" />
			<text style="margin-left: 10rpx;color: gray;">护尔爱导航</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				CustomBar: this.CustomBar,
				cuIcon: [{
						name: '济源市人民医院',
						isShow: true,
						imgSrc: '../../static/logo.png',
						Asrc: 'https://jyrm.huerai.com/wechat/home?appidparam=a8ba2d75-baf6-40d9-9790-f0fcd70735e0&basekey=2d60565f-d99f-47a0-ab2f-68fe25b6c06b&clienttype=2',
						lat: 35.097818,
						lng: 112.586854
					},
					// {
					// 	name: '洛阳市第一人民医院(陇海院区)',
					// 	isShow: true,
					// 	imgSrc:'../../static/sanyuan.png',
					// 	Asrc: 'https://map.huerai.com/wechat/home',
					// 	lat: 34.699184,
					// 	lng: 112.474704
					// }
				],
				goNav: '',
			};
		},

		onLoad() {
			console.log("111111111111111111");
			// 检查权限状态
			uni.getSetting({
				success: (res) => {
					const authSetting = res.authSetting;
					if (!authSetting['scope.bluetooth'] || !authSetting['scope.userLocation']) {
						console.log("222222");
						this.requestPermissions(); // 请求权限
					} else {
						console.log("333333");
						this.initBluetooth(); // 已有权限，初始化蓝牙
					}
				}
			});
		},
		methods: {
			searchIcon(e) {
				let key = e.detail.value.toLowerCase();
				let list = this.cuIcon;
				for (let i = 0; i < list.length; i++) {
					let a = key;
					let b = list[i].name.toLowerCase();
					if (b.search(a) != -1) {
						list[i].isShow = true
					} else {
						list[i].isShow = false
					}
				}
				this.cuIcon = list
			},
			goNavs(a) {
				let data = encodeURIComponent(a)
				uni.navigateTo({
					url: '/pages/basics/layoutcom/daohang?goSrc=' + data
				})
			},
			goDing(a) {

				uni.openLocation({
					latitude: a.lat,
					longitude: a.lng,
					success: function() {
						console.log('success');
					}
				});
			},



			// 请求权限方法
			requestPermissions() {
				const permissions = ['scope.bluetooth', 'scope.userLocation'];
				permissions.forEach(permission => {
					uni.authorize({
						scope: permission,
						success: () => console.log(`${permission} 授权成功`),
						fail: (err) => {
							console.error(`${permission} 授权失败`, err);
							this.handleAuthReject(); // 处理拒绝逻辑
						}
					});
				});
			},

			initBluetooth() {
				uni.openBluetoothAdapter({
					success: (res) => {
						console.log("蓝牙初始化成功");
						// 后续可执行扫描、连接等操作
					},
					fail: (err) => {
						console.error("蓝牙初始化失败", err);
						if (err.errCode === 10001) { // 系统蓝牙未开启
							this.showEnableBluetoothModal();
						}
					}
				});
			},

			handleAuthReject() {
				uni.showModal({
					title: "权限未开启",
					content: "需要蓝牙和定位权限才能使用该功能，是否去设置页开启？",
					confirmText: "去设置",
					success: (res) => {
						if (res.confirm) {
							uni.openSetting(); // 跳转至小程序设置页
						}
					}
				});
			}


		},
		onShareAppMessage() {
			return {
				title: "院区导航",
				path: `pages/basics/layout`,
			};
		},
	}
</script>

<style>
	page {
		padding-top: 50px;
	}
</style>